package com.ip.user.api;

import com.ip.user.models.dto.SliderCaptchaDTO;
import com.ip.user.models.service.SliderCaptchaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 滑块验证码控制器
 * author 刘丰源
 */
@RestController
@RequestMapping("/api/captcha")
@CrossOrigin
public class SliderCaptchaController {
    
    @Autowired
    private SliderCaptchaService sliderCaptchaService;
    
    /**
     * 获取滑块验证码
     */
    @PostMapping("/slider")
    public ResponseEntity<?> getSliderCaptcha() {
        Map<String, Object> captchaResult = sliderCaptchaService.generateCaptcha();
        
        if (captchaResult.containsKey("success") && (Boolean) captchaResult.get("success")) {
            return ResponseEntity.ok(captchaResult);
        } else {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", captchaResult.get("message") != null ? captchaResult.get("message") : "获取验证码失败");
            return ResponseEntity.badRequest().body(response);
        }
    }
    
    /**
     * 验证滑块验证码
     */
    @PostMapping("/verify")
    public ResponseEntity<?> verifySliderCaptcha(@RequestBody SliderCaptchaDTO sliderCaptchaDTO) {
        boolean isValid = sliderCaptchaService.verifyCaptcha(sliderCaptchaDTO);
        
        Map<String, Object> response = new HashMap<>();
        if (isValid) {
            response.put("success", true);
            response.put("message", "验证成功");
        } else {
            response.put("success", false);
            response.put("message", "验证失败");
        }
        
        return ResponseEntity.ok(response);
    }
}